<?php

namespace Platform\Api;

use PhalApi\Api;
use Platform\Domain\Menu as MenuDomain;
use PhalApi\Exception\InternalServerErrorException;

/**
 * 菜单模块
 */
class Menu extends Api {
    
    public function getRules() {
        return array(
            'menuList' => array(
            ),
            'selectMenuList'=> array(
            ),
            'getRoleList'=> array(
            ),
            'addMenu'=> array(
                'parentId' => array('name' => 'parentId', 'require' => false, 'desc' => '上级菜单'),
                'title' => array('name' => 'title', 'require' => true, 'desc' => '标题'),
                'path' => array('name' => 'path', 'require' => true, 'desc' => '访问路径'),
                'url' => array('name' => 'url', 'require' => true, 'desc' => '跳转路径'),
                'role_ids' => array('name' => 'role_ids', 'require' => false, 'desc' => '关联角色'),
                'name' => array('name' => 'name', 'require' => true, 'desc' => '名称'),
                'sort_num' => array('name' => 'sort_num', 'require' => true, 'desc' => '排序'),
                'icon' => array('name' => 'icon', 'require' => false, 'desc' => '图标'),
            ),
            'menuDetail'=> array(
                'id' => array('name' => 'id', 'require' => true, 'desc' => '菜单id'),
            ),
            'updateMenu'=> array(
                'id' => array('name' => 'id', 'require' => true, 'desc' => '要修改的菜单id'),
                'parentId' => array('name' => 'parentId', 'require' => false, 'desc' => '上级菜单'),
                'title' => array('name' => 'title', 'require' => true, 'desc' => '标题'),
                'path' => array('name' => 'path', 'require' => true, 'desc' => '访问路径'),
                'url' => array('name' => 'url', 'require' => true, 'desc' => '跳转路径'),
                'role_ids' => array('name' => 'role_ids', 'require' => false, 'desc' => '关联角色'),
                'name' => array('name' => 'name', 'require' => true, 'desc' => '名称'),
                'sort_num' => array('name' => 'sort_num', 'require' => true, 'desc' => '排序'),
                'icon' => array('name' => 'icon', 'require' => false, 'desc' => '图标'),
            ),
            'deleteMenu'=> array(
                'id' => array('name' => 'id', 'require' => true, 'desc' => '菜单id'),
            ),
        );
    }

    /**
     * 获取菜单列表
     * @desc 获取菜单列表
     */
    public function menuList() {
        $domain = new MenuDomain();
        $menus = $domain->menuList();
        return $menus;
    }

    /**
     * 获取添加/编辑时需要的菜单列表
     * @desc 获取添加/编辑时需要的菜单列表
     *
     */
    public function selectMenuList() {
        $domain = new MenuDomain();
        $menus = $domain->selectMenuList();
        return $menus;
    }

    /**
     * 菜单获取当前角色列表
     * @desc 菜单获取当前角色列表
     */
    public function getRoleList() {
        $domain = new MenuDomain();
        $list = $domain->getRoleList();
        return $list;
    }

    /**
     * 添加菜单
     * @desc 添加菜单
     */
    public function addMenu() {
        $domain = new MenuDomain();
        //列出所有需要添加的参数
        $insertData = array();
        $insertData['parent_id'] = $this->parentId?$this->parentId:0;
        $insertData['title'] = $this->title;
        $insertData['path'] = $this->path;
        $insertData['url'] = $this->url;
        $insertData['role_ids'] = $this->role_ids?$this->role_ids:0;
        $insertData['name'] = $this->name;
        $insertData['sort_num'] = $this->sort_num;
        $insertData['icon'] = $this->icon?$this->icon:'';
        return $domain->addMenu($insertData);
    }

    /**
     * 获取菜单详情
     * @desc 获取菜单详情
     */
    public function menuDetail() {
        $domain = new MenuDomain();
        $id = $this->id;
        $select='id,title,icon,path,sort_num,parent_id as parentId,url,name';
        return $domain->menuDetail($id,$select);
    }

    /**
     * 编辑提交菜单
     * @desc 编辑提交菜单
     */
    public function updateMenu() {
        $domain = new MenuDomain();
        //列出所有需要添加的参数
        $id = $this->id;
        $updateData = array();
        $updateData['parent_id'] = $this->parentId?$this->parentId:0;
        $updateData['title'] = $this->title;
        $updateData['path'] = $this->path;
        $updateData['url'] = $this->url;
        $updateData['role_ids'] = $this->role_ids?$this->role_ids:0;
        $updateData['name'] = $this->name;
        $updateData['sort_num'] = $this->sort_num;
        $updateData['icon'] = $this->icon?$this->icon:'';
        return $domain->updateMenu($id,$updateData);
    }

    /**
     * 删除菜单
     * @desc 删除菜单
     */
    public function deleteMenu() {
        $domain = new MenuDomain();
        $id = $this->id;
        return $domain->deleteMenu($id);
    }
}
